# -- coding: utf-8 --
# file: fa2.py
#

from collections import OrderedDict
import pyperclip as clp
import str_operate
import re


def setup(file='./setup.ini'):
    """
    读入配置文件

    Args:
        file (字符串, optional): [description]. Defaults to './setup.ini'.

    Returns:
        [字符串元组]: [description]
    """
    f = open(file, 'r')
    xls = str()
    sheet = str()
    pos = str()
    while 1:
        line = f.readline()
        if line[0] == '#':
            continue
        if xls == '':
            xls = line
        elif sheet == '':
            sheet = line
        else:
            pos = line
    return (xls, sheet, pos)
# 获得待查找的120个数字的字符串数组


if __name__ == '__main__':

    lines = clp.paste().split('\n')
    search_arr = str_operate.splitString(lines[0], '[ \t\r\n]+')
    if not str_operate.arrIsOK(search_arr):
        # print("输入的内容不是1~49之间的数字。")
        exit("输入的内容不是1~49之间的数字。")
    else:
        if len(lines) < 120*80:
            # print("")
            exit("复制的行数不够120x80行。")

        std_lst = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        result = ""
        lineNO = 1

        # 外循环loop['extrinsic']（120）次
        loop = {'extrinsic': 120, 'internal': 80}
        for x in range(0, loop['extrinsic']):
            # 将数组lst的各值置零
            lst = std_lst[:]

            # 内循环loop['internal']（80）次
            for y in range(0, loop['internal']):
                # 取出一行分解成组
                s = str_operate.splitString(lines[lineNO], '[ \t\r\n]+')
                lineNO += 1

                # 从数组中找出1~49的次数记录在lst中
                for key in range(1, 50):
                    if key < 10:
                        lst[key] += str_operate.time(s, '0' + str(key))
                    else:
                        lst[key] += str_operate.time(s, str(key))

            srt_lst = sorted(enumerate(lst), key=lambda x: x[1])

            dct = OrderedDict()
            for z in range(1, 50):
                if srt_lst[z][1] in dct:
                    dct[srt_lst[z][1]].append(srt_lst[z][0])
                else:
                    dct[srt_lst[z][1]] = [srt_lst[z][0]]

            cmp = int(search_arr[x])
            while cmp >= 1:
                item = dct.popitem()
                if len(item[1]) >= cmp:
                    r = str(item[1][cmp - 1])
                    if len(r) == 1:
                        r = '0' + r
                    result += r + '\n'
                    break
                else:
                    cmp -= len(item[1])

        clp.copy(result)
